
window.onload= function(){
	layui.use('form', function() {
				var form = layui.form;
			});
    var ycode="";
    var username="";
    var school="";
    var email="";
    var password="";

    function getXmlHttp(){
        var xmlHttp;
        try{
            xmlHttp = new XMLHttpRequest();
        }catch(e){
            try{
                xmlHttp= new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
                try{
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }catch(e){
                }
            }
        }
        return xmlHttp;

    }
    var sbu = document.getElementById("mybu");
    var mybutton = document.getElementById("mybutton");
    mybutton.onclick=function(){
        username = document.getElementById("LAY-user-login-username").value;
        school = document.getElementById("LAY-user-login-schoolid").value;
        email = document.getElementById("LAY-user-login-email").value;
        password = document.getElementById("LAY-user-login-password").value;
        if (!(/^\d{10}$/.test(school))){
            layer.msg("ID为10位数字", {
                icon: 2,
                time: 2000
            });
            return false;
        }
        if (username==="" || school==="" || email==="" || password ===""){
            layer.msg("请完善信息", {
                icon: 2,
                time: 2000
            });
            return false;
        }
        reg=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;
        if (!reg.test(email)) {
            layer.msg("邮箱格式不正确", {
                icon: 2,
                time: 2000
            });
            return false;
        }
        mybutton.setAttribute("class","layui-btn layui-btn-disabled");
        var request = getXmlHttp();

        request.open("POST","/regeist/sendEmail",true);
        request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        request.send("email="+email+"&username="+username);
       
        layer.msg("验证码已发送，请注意查收，并在5分钟内完成验证操作", {
  				icon: 1,
  				time: 2000 
        });
       request.onreadystatechange=function()
        {
            if (request.readyState==4 && request.status==200)
            {
                ycode=request.responseText;
            }
        }

       startTime();
       return false;
    }


    document.getElementById("LAY-user-login-email").onfocus=function(){
        var school = document.getElementById("LAY-user-login-schoolid").value;
        if (!(/^\d{10}$/.test(school))){
            layer.msg("ID为10位数字", {
                icon: 2,
                time: 2000
            });
            return false;
        }

        var request = getXmlHttp();
        request.open("POST","/regeist/isExit",true);
        request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        request.send("school="+school);
        request.onreadystatechange=function()
        {
            if (request.readyState==4 && request.status==200)
            {
                var mess = request.responseText;
                if (mess==="exit"){
                    layer.msg("该账号已存在,请登陆或更换", {
                        icon: 2,
                        time: 4000
                    });
                }
            }
        }
    }
  sbu.onclick=function() {
        var code = document.getElementById("LAY-user-login-vercode").value;
        if(code===ycode){
            var req = getXmlHttp();

            req.open("POST","/regeist",true);
            req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            req.send("email="+email+"&schoolid="+school+
                "&username="+username+"&password="+password);

            /*显示信息*/
            layer.msg("注册成功", {
                icon: 1,
                time: 2000
            },function () {
                location.href = "index.html";
            });
            return true;
        }
      layer.msg("验证码错误", {
          icon: 2,
          time: 2000
      });
        return false;
    }


}

var h=60;
var t;

function startTime()
{
    h=h-1;
    if(!h){
        mybutton.setAttribute("class","layui-btn layui-btn-fluid");
    document.getElementById('mybutton').innerHTML="获取验证码";
    h=60;
    return false;
}
    document.getElementById('mybutton').innerHTML=h+"秒后重送";
    t=setTimeout('startTime()',1000);

}

